/*
VARIABLE CREATION

Data 	: A1_merged_jobb_firm_pers.dta
Folder 	: 
Date	:

Creator		: Jonas Cederlof	(JC)
Description : Creating variables.
Notes:

*/

********************************************************************************

clear
set more		 off
cap   log close 	_all

log using 	"../log/A3_generate_variable.log", replace 
use 		"$datapath/A2_cleaned_masterdata.dta"


desc annual_ear annual_w3 annual_ear_atfirm annual_w1 ///
			annual_ear_otherfirm annual_w2 ///
			monthly_w1 monthly_w2 Eear1-Eear10
			
{ // Inflation adjust earnings and wages to 2005 SEK
*===============================================================================
*NOTE: Wages from the wage sruvey are already CPI adjusted in A1_clean_wagestruc.do
merge m:1 year 	using "$datapath/cpi_jc.dta"
drop if _merge==2
drop 	_merge

foreach var of varlist 	annual_ear annual_w3 annual_ear_atfirm annual_w1 ///
			annual_ear_otherfirm annual_w2 ///
			monthly_w1 monthly_w2 Eear*{
	replace `var' = `var'/cpi
}

}
*
{ // Generate: Age at notification of displacement
*===============================================================================
gen 		age_def  = (notdate_def - birth)/12
lab var 	age_def	  "Age in years with decimals at notification (based on inkom_dat)"

}
*
{ // Generate: Recode and dummy out level of education
*===============================================================================
recode utbniv (1=1 "Primary School") (2=1 "Primary School") (3=2 "High School") (4=2 "High School") (5=3 "College") (6=3 "College") (7=4 "P.hD"), gen(utbniv2)
qui tab utbniv2 , gen(educ)
drop utbniv2*

}
*

{ // Generate: Annual earnings and wage one year prior to notification
*===============================================================================
*Note: 	Annual_ear is constant within a year so taking the n-12 observation 
*	is always the same as taking the year before notification.
*	Note also that since this a pre notification variable it need to be 
*	defined seperatly for the two possible notification dates (_def and _DS)

bys lopnr (date) : gen xannual_ear_prenot_def = annual_ear[_n-12] if eventtime_def==0
bys lopnr (date) : gen xmanl_prenot_def = manl_notfirm_specific[_n-12] if eventtime_def==0

fastmax xannual_ear_prenot_def, by(lopnr) name(annual_ear_prenot_def)
fastmax xmanl_prenot_def, by(lopnr) name(manl_prenot_def)

drop x*
}
*

{ // Fix other covarites to t=0
*===============================================================================
*Note: variables immigrant, female and tenureatnot are already fixed over time 
forvalues i=1(1)4{
	gen X_temp = educ`i' if eventtime_def==0
	bys lopnr : egen X_tempmax = max(X_temp)
	replace educ`i' = X_tempmax
	drop X_*
}

}
*

{ // Generate : Date year-quarter and year-month variabels (for FE's)
*===============================================================================
gen ym_def = mofd(inkom_dat)
format ym_def %tm
gen yq_def = qofd(inkom_dat)
format yq*  %tq

lab var ym_def	"FE year month (based on inkom_dat)"
lab var yq_def	"FE year quarter (based on inkom_dat)"

}
*


{ // Generate: Tenure at notification
*===============================================================================
gen xtemp = tenure1 if eventtime_def==0
fastmax xtemp, by(lopnr) name(tenureatnot) 
drop xtemp
}
*


{ // Merge on white-collar indicator (created in A3_WCindicator.do)
*===============================================================================
merge m:1 lopnr year using "$datapath/A3_WCindicator.dta", nogen
}
*






*Save data
compress
save "$datapath/A3_gen.dta",replace
